/**
 * This is a simple module for kobject test.And it is licensed under the terms
 * of GNU/GPL v3 or later.
 * Wang Sen (C) <senwang@linux.vnet.ibm.com>
 * Wang Sen (C) <kelvin.xupt@gmail.com>
 * 2012.3.23
 */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/list.h>
#include <linux/stat.h>
#include <linux/string.h>

#define BUF_SIZE 128

MODULE_LICENSE("GPL");

static char asset[BUF_SIZE] = "Hello Kitty";

static void test_release(struct kobject *kobj);
static ssize_t test_show(struct kobject *, struct attribute *, char *);
static ssize_t test_store(struct kobject *, struct attribute *, const char *, size_t);

static void test_release(struct kobject *kobj)
{
	if (kobj == NULL) {
		return;
	}
	
	printk("------[kobject_test]Hello,test_release is dancing\n");
}

static ssize_t test_show(struct kobject *kobj, struct attribute *attr, char *buf) 
{
	sprintf(buf, "asset:%s\nkobject_name:%s\n", asset,kobj->name);
	return strlen(buf);
}

static ssize_t test_store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t size) 
{
	if (strlen(buf) > BUF_SIZE) {
		return E2BIG;
	}
	memset(asset, 0, BUF_SIZE);
	sprintf(asset, "%s", buf);
	return strlen(buf);
}

struct attribute test_attr = {
	.name = "info",
	.mode = S_IRWXUGO,
}; 

struct attribute test_attr_1 = {
	.name = "addr",
	.mode = S_IRWXUGO,
}; 

struct attribute *attr_array[] = {
	&test_attr,
	&test_attr_1,
	NULL
};

struct sysfs_ops test_sysfs_ops = {
	.show = test_show,
	.store = test_store,
};

struct kobject test_kobject;
struct kobj_type test_kobj_type= {
	.release = test_release,
	.sysfs_ops = &test_sysfs_ops,
	.default_attrs = attr_array,
};

static int __init kobj_test_init(void)
{
	printk("------kobject init...");
	kobject_init_and_add(&test_kobject, &test_kobj_type, NULL, "test_object_of_kelvin");
	return 0;
}

static void __exit kobj_test_exit(void)
{
	kobject_put(&test_kobject);
	printk("------See you");
}

module_init(kobj_test_init);
module_exit(kobj_test_exit);

MODULE_AUTHOR("Kelvin Wang <senwang@linux.vnet.ibm.com>");
